<?php

namespace Api\Service;

use AllowDynamicProperties;
use GuzzleHttp\Client;

#[AllowDynamicProperties] class YidaService
{
    public function __construct()
    {
        $this->domain = 'https://www.yida178.cn/prod-api/thirdApi/execute';
        $this->username = env('YIDAAPPID');
        $this->secret = env('YIDASECRET');
    }

    private function getsign($timestamp)
    {
        $sign_Array = [
            "privateKey" => $this->secret,
            "timestamp" => $timestamp,
            "username" => $this->username
        ];

        return strtoupper(MD5(json_encode($sign_Array, 320)));
    }

    public function checkChannel($data)
    {

        $post = [];
        $post['customerType'] = $data['channelTag'] == '智能' ? 'kd' : 'ky';
        $post['thirdNo'] = "k" . date("Ymd") . uniqid();
        $post['senderProvince'] = $data['senderProvince'];
        $post['senderCity'] = $data['senderCity'];
        $post['senderDistrict'] = $data['senderCounty'];
        $post['senderAddress'] = $data['senderLocation'];
        $post['senderName'] = $data['sender'];
        $post['senderMobile'] = $data['senderMobile'];
        $post['receiveProvince'] = $data['receiveProvince'];
        $post['receiveCity'] = $data['receiveCity'];
        $post['receiveDistrict'] = $data['receiveCounty'];
        $post['receiveAddress'] = $data['receiveLocation'];
        $post['receiveName'] = $data['receiver'];
        $post['receiveMobile'] = $data['receiverMobile'];
        $post['goods'] = $data['itemName'] ?? '日用品';
        $post['packageCount'] = $data['packageCount'];
        $post['weight'] = $data['weight'];
//        $post['vloumLong'] = $data['vloumLong'] ?? '';
//        $post['vloumWidth'] = $data['vloumWidth'] ?? '';
//        $post['vloumHeight'] = $data['vloumHeight'] ?? '';
        $post['guaranteeValueAmount'] = $data['insured'] ?? '';
        $post['onlinePay'] = 'Y';
        $time = (string)intval(microtime(1) * 1000);
        $body = [
            "apiMethod"        => 'SMART_PRE_ORDER',
            "businessParams"   => (object)$post,
            "sign"             => $this->getsign($time),
            "timestamp"        =>  $time,
            "username"         => $this->username
        ];
        $client=new Client(['timeout' => 5, 'verify' => false]);
//设置headers头
        $headers = ['Content-Type' => "application/json"];
        $response = $client->post($this->domain, [
            'headers' => $headers,
            'body' => json_encode($body,320)
        ]);
        $json =  $response->getBody()->getContents();
        if($response->getStatusCode() == 200){
            $result = json_decode($json,true);
            $data = $result['data'];
            return $data;
        }

    }

    public function createOrder($data)
    {
        $post = [];
        $post['customerType'] = $data['channelTag'] == '智能' ? 'kd' : 'ky';
        $post['thirdNo'] =  $data['thirdNo'];
        $post['senderProvince'] = $data['senderProvince'];
        $post['senderCity'] = $data['senderCity'];
        $post['senderDistrict'] = $data['senderCounty'];
        $post['senderAddress'] = $data['senderLocation'];
        $post['senderName'] = $data['sender'];
        $post['senderMobile'] = $data['senderMobile'];
        $post['receiveProvince'] = $data['receiveProvince'];
        $post['receiveCity'] = $data['receiveCity'];
        $post['receiveDistrict'] = $data['receiveCounty'];
        $post['receiveAddress'] = $data['receiveLocation'];
        $post['receiveName'] = $data['receiver'];
        $post['receiveMobile'] = $data['receiverMobile'];
        $post['goods'] = $data['itemName'] ?? '日用品';
        $post['packageCount'] = $data['packageCount'];
        $post['weight'] = $data['weight'];
        $post['remark'] = $data['billRemark'];
        $post['channelType'] = 'yida';
        $post['deliveryType'] = $data['deliveryType'];
        $post['channelId'] = $data['channelId'];
//        $post['channelId'] = ;
        $list = [];
        if(!empty($data['insured']) && $data['insured'] != 0){
            $insure['type'] = 1;
            $insure['value'] = $data['insured'];
            $list[] = $insure;
            $post['orderIncrements'] = json_encode($list,320);
        }
        if(!empty($data['vloumLong']) &&  !empty($data['vloumWidth']) && !empty($data['vloumHeight'])){
            $post['vloumLong'] = $data['vloumLong'];
            $post['vloumWidth'] = $data['vloumWidth'];
            $post['vloumHeight'] = $data['vloumHeight'];
        }

        $time = (string)intval(microtime(1) * 1000);
        $body = [
            "apiMethod"        => 'SUBMIT_ORDER_V2',
            "businessParams"   => (object)$post,
            "sign"             => $this->getsign($time),
            "timestamp"        =>  $time,
            "username"         => $this->username
        ];
        $client=new Client(['timeout' => 5, 'verify' => false]);
//设置headers头
        $headers = ['Content-Type' => "application/json"];
        $response = $client->post($this->domain, [
            'headers' => $headers,
            'body' => json_encode($body,320)
        ]);
        if($response->getStatusCode() == 200){
            return $response->getBody()->getContents();
        }


        return true;

    }

    public function cancelOrder($data)
    {
        $time = (string)intval(microtime(1) * 1000);
        $post['deliveryId'] = $data['waybill'];
        $body = [
            "apiMethod"        => 'CANCEL_ORDER',
            "businessParams"   => (object)$post,
            "sign"             => $this->getsign($time),
            "timestamp"        =>  $time,
            "username"         => $this->username
        ];
        $client=new Client(['timeout' => 5, 'verify' => false]);
//设置headers头
        $headers = ['Content-Type' => "application/json"];
        $response = $client->post($this->domain, [
            'headers' => $headers,
            'body' => json_encode($body,320)
        ]);
        return $response->getBody()->getContents();
    }

    public function getRoute($data)
    {
        $time = (string)intval(microtime(1) * 1000);
        $post['deliveryId'] = $data['waybill'];
        $body = [
            "apiMethod"        => 'DELIVERY_TRACE',
            "businessParams"   => (object)$post,
            "sign"             => $this->getsign($time),
            "timestamp"        =>  $time,
            "username"         => $this->username
        ];
        $client=new Client(['timeout' => 5, 'verify' => false]);
//设置headers头
        $headers = ['Content-Type' => "application/json"];
        $response = $client->post($this->domain, [
            'headers' => $headers,
            'body' => json_encode($body,320)
        ]);
        return $response->getBody()->getContents();
    }

    public function getBalance()
    {
        $data['userMobile'] = 13066660642;
        $time = (string)intval(microtime(1) * 1000);
        $body = [
            "apiMethod"        => 'ACCOUNT_BALANCE',
            "businessParams"   => (object)$data,
            "sign"             => $this->getsign($time),
            "timestamp"        =>  $time,
            "username"         => $this->username
        ];
        $client=new Client(['timeout' => 5, 'verify' => false]);
//设置headers头
        $headers = ['Content-Type' => "application/json"];
        $response = $client->post($this->domain, [
            'headers' => $headers,
            'body' => json_encode($body,320)
        ]);
        $json =  $response->getBody()->getContents();
        $result = json_decode($json,true);
        return $result['data']['accountBalance'];
    }


}
